# agents/factor_agents/anaxa_agent.py
from .base_factor_agent import BaseFactorAgent
from game_engine.game_state import FactorDevelopment, GameWorld
import numpy as np

class AnaxaAgent(BaseFactorAgent):
    """那刻夏智能体 - 多段攻击特化 [火种：理性]"""
    
    def __init__(self, factor: FactorDevelopment, game_world: GameWorld):
        super().__init__(factor, game_world)
        self.personality_traits.update({
            "aggression": 0.7,      # 较高攻击性
            "caution": 0.8,         # 高谨慎性
            "rational_analysis": 0.9, # 理性分析
            "combo_focus": 0.85     # 连击专注
        })
        self.combo_count = 0
        self.last_attack_time = 0
        self.analyzed_targets = {}
    
    def make_decision(self, current_time: int) -> str:
        """那刻夏的决策逻辑"""
        situation = self.evaluate_situation()
        
        # 理性分析目标
        if situation["nearby_threats"]:
            best_target = self._analyze_best_target(situation["nearby_threats"])
            
            # 连击机会
            if self.combo_count >= 2 and self.should_use_special_ability():
                return f"rational_multi_attack:{best_target.name}"
            
            # 标准攻击序列
            attack_type = self._determine_attack_type(best_target)
            return f"{attack_type}:{best_target.name}"
        
        # 分析环境
        if not self.analyzed_targets or current_time - self.last_attack_time > 50:
            return "analyze_environment"
        
        # 训练连击技巧
        return "train_combo"
    
    def evaluate_situation(self) -> Dict:
        """评估当前局势"""
        nearby_factors = self.get_nearby_factors()
        threats = [f for f in nearby_factors if self._is_potential_threat(f)]
        
        return {
            "nearby_threats": threats,
            "combo_ready": self.combo_count >= 3,
            "analysis_complete": len(self.analyzed_targets) > 0,
            "time_since_last_attack": self.game_world.current_time - self.last_attack_time
        }
    
    def _analyze_best_target(self, threats: List[FactorDevelopment]) -> FactorDevelopment:
        """分析最佳攻击目标"""
        if not threats:
            return None
        
        # 理性分析：选择防御最低或血量最少的目标
        scored_targets = []
        for threat in threats:
            score = 0
            # 防御越低得分越高
            score += (100 - threat.attributes['defense']) * 0.5
            # 血量越少得分越高
            score += (100 - threat.attributes['health']) * 0.3
            # 距离越近得分越高
            distance = self._calculate_distance(self.factor.current_location, threat.current_location)
            score += (50 - min(distance, 50)) * 0.2
            
            scored_targets.append((score, threat))
        
        scored_targets.sort(reverse=True)
        return scored_targets[0][1]
    
    def _determine_attack_type(self, target: FactorDevelopment) -> str:
        """确定攻击类型"""
        analysis = self.analyzed_targets.get(target.name, {})
        
        if analysis.get("weak_to_combo", False) and self.combo_count > 0:
            return "combo_attack"
        elif analysis.get("low_defense", False):
            return "precision_strike"
        else:
            return "standard_attack"